<?php
/**
* 提现自动转账记录
*/
class Transfer_record_model extends CI_Model
{
    private $_table  = 'transfer_record';
    private $_table1 = 'withdraw';

    /**
     * 总页数
     */
    public function total($params)
    {
        $params = array_map('trimArray', $params);
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'transfer_record.transfer_id = withdraw.transfer_id', 'INNER');

        if (!empty($params['request_sn'])) { //建行交易请求编号
            $this->db->where_in("transfer_record.request_sn", $params['request_sn']);
        }

        if (!empty($params['transfer_id'])) { //交易记录编号
            $this->db->where_in("withdraw.transfer_id", $params['transfer_id']);
        }

        if (!empty($params['transferStatus'])) { //交易状态
            $this->db->where_in("transfer_record.status", $params['transferStatus']);
        }

        if ( ! empty($params['scenery_name'])) {
            $this->db->where("((`scenery_name` LIKE '%{$params['scenery_name']}%') OR (`sid`='{$params['scenery_name']}'))");
        }

        if ( ! empty($params['user_name'])) {
            $this->db->where("((`user_name` LIKE '%{$params['user_name']}%') OR (`uid`='{$params['user_name']}'))");
        }

        if ( ! empty($params['mobile_phone'])) {
            $this->db->like("mobile_phone", $params['mobile_phone']);
        }

        if ( ! empty($params['start_date'])) {
            $this->db->where("transfer_record.updated_at >=", $params['start_date']. ' 00:00:01');
        }

        if ( ! empty($params['end_date'])) {
            $this->db->where("transfer_record.updated_at <=", $params['end_date']. ' 23:59:59');
        }

        if ( ! empty($params['cardholder'])) {
            $this->db->like("cardholder", $params['cardholder']);
        }

        if ( ! empty($params['is_from'])) {
            $this->db->where_in('withdraw.is_from', $params['is_from']);
        }

        return $this->db->count_all_results();
    }

    /**
     * 查询提现记录
     */
    public function page_list($params, $page_num, $num)
    {
        $params =array_map('trimArray', $params);
        $this->db->select('transfer_record.transfer_id, transfer_record.request_sn, transfer_type, total_amount, total_num, success_num, success_amount, transfer_record.status AS transfer_record_status, transfer_record.state AS transfer_record_state, message, response_content, transfer_record.created_at AS transfer_created_at, start_time, transfer_record.updated_at AS transfer_updated_at, withdraw_id, withdraw.withdraw_id, withdraw.uid, sid, scenery_name, user_name, amount, amount_carry, is_from, withdraw.state, remark, withdraw.created_at AS withdraw_created_at, withdraw.updated_at AS withdraw_updated_at, bank_id, bank_card, cardholder, pin, bank_name, mobile_phone, audit_status, transfer_status, poundage, transfer_amount');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'transfer_record.transfer_id = withdraw.transfer_id', 'INNER');

        if (!empty($params['request_sn'])) { //建行交易请求编号
            $this->db->where_in("transfer_record.request_sn", $params['request_sn']);
        }

        if (!empty($params['transfer_id'])) { //交易记录编号
            $this->db->where_in("withdraw.transfer_id", $params['transfer_id']);
        }

        if (!empty($params['transferStatus'])) { //交易状态
            $this->db->where_in("withdraw.transfer_status", $params['transferStatus']);
        }

        if ( ! empty($params['scenery_name'])) {
            $this->db->where("((`scenery_name` LIKE '%{$params['scenery_name']}%') OR (`sid`='{$params['scenery_name']}'))");
        }

        if ( ! empty($params['user_name'])) {
            $this->db->where("((`user_name` LIKE '%{$params['user_name']}%') OR (`uid`='{$params['user_name']}'))");
        }

        if ( ! empty($params['mobile_phone'])) {
            $this->db->like("mobile_phone", $params['mobile_phone']);
        }

        if ( ! empty($params['start_date'])) {
            $this->db->where("transfer_record.updated_at >=", $params['start_date']. ' 00:00:01');
        }

        if ( ! empty($params['end_date'])) {
            $this->db->where("transfer_record.updated_at <=", $params['end_date']. ' 23:59:59');
        }

        if ( ! empty($params['cardholder'])) {
            $this->db->like("cardholder", $params['cardholder']);
        }

        if ( ! empty($params['is_from'])) {
            $this->db->where_in('withdraw.is_from', $params['is_from']);
        }

        $this->db->order_by('transfer_id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    /**
     * 部分更新 更新交易状态
     */
    public function updateState($transferId, $params)
    {
        $this->db->where('transfer_id', $transferId);
        return $this->db->update($this->_table, $params);
    }

    /**
     * 强势插入
     */
    public function insert($params)
    {
        $data = array(
            'request_sn'       => $params['request_sn'],
            'transfer_type'    => $params['transfer_type'],
            'total_amount'     => $params['total_amount'],
            'total_num'        => $params['total_num'],
            'success_amount'   => $params['success_amount'],
            'success_num'      => $params['success_num'],
            'status'           => isset($params['status']) ? $params['status'] : 1,
            'state'            => 1,
            'flag'             => $params['flag'],
            'message'          => isset($params['message']) ? $params['message'] : '',
            'response_content' => isset($params['response_content']) ? $params['response_content'] : '',
            'start_time'       => $params['start_time'],
            'created_at'       => date('Y-m-d H:i:s'),
            'updated_at'       => date('Y-m-d H:i:s')
        );
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }

    /**
     * 查找建行记录
     */
    public function getOneRecord($params)
    {
        $this->db->select('transfer_id, request_sn, transfer_type, status, state, total_amount, total_num, actual_amount, actual_num, success_amount, success_num, flag, send_file, message, response_content, start_time, created_at, updated_at, ori_file');
        $this->db->from($this->_table);
        if ( ! empty($params['transfer_id'])) {
            $this->db->where('transfer_id', $params['transfer_id']);
        }
        if ( ! empty($params['request_sn'])) {
            $this->db->where('request_sn', $params['request_sn']);
        }

        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 查找最后一个执行的记录
     */
    public function getLastTransfer($params = array())
    {
        $this->db->select('transfer_id, request_sn, transfer_type, status, state, total_amount, total_num, actual_amount, actual_num, success_amount, success_num, flag, send_file, message, response_content, start_time, created_at, updated_at, ori_file');
        $this->db->from($this->_table);
        $this->db->where('flag', 1);
        if ( ! empty($params['transfer_type'])) {
            $this->db->where_in('transfer_type', $params['transfer_type']); //批量交易
        }
        if ( ! empty($params['transfer_id'])) {
            $this->db->where('transfer_id', $params['transfer_id']); //交易编号
        }
        $this->db->order_by('transfer_id', 'DESC');
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 查找用户详情
     */
    public function findWithdrawInfo($withdrawId)
    {
        $this->db->select('transfer_record.transfer_id, transfer_record.request_sn, total_amount, total_num, success_num, success_amount, transfer_record.status AS transfer_status, message, transfer_record.created_at AS transfer_created_at, transfer_record.updated_at AS transfer_updated_at, start_time, withdraw.withdraw_id, withdraw.uid, amount, amount_carry, is_from, withdraw.state, bank_id, bank_card, cardholder, bank_name, mobile_phone, audit_status, user_name, pin, sid, scenery_name, withdraw.updated_at AS withdraw_updated_at');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'transfer_record.transfer_id = withdraw.transfer_id', 'INNER');

        $this->db->where('is_from', 3); //扫码提现
        $this->db->where("withdraw.withdraw_id", $withdrawId); //转账记录编号
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 总页数
     */
    public function transferTotal($params)
    {
        $params = is_array($params) ? array_map('trimArray', $params) : '';
        $this->db->select('*');
        $this->db->from($this->_table);

        $this->db->where('transfer_type', 1); // 1,建行全国批量代发代扣交易(6W2100)
        if (!empty($params['request_id'])) {
            $this->db->where_in("transfer_record.request_sn", $params['request_id']);
        }

        if (!empty($params['transferStatus'])) { //交易状态
            $this->db->where_in("transfer_record.status", $params['transferStatus']);
        }

        if ( ! empty($params['start_date'])) {
            $this->db->where("transfer_record.updated_at >=", $params['start_date']. ' 00:00:01');
        }

        if ( ! empty($params['end_date'])) {
            $this->db->where("transfer_record.updated_at <=", $params['end_date']. ' 23:59:59');
        }

        return $this->db->count_all_results();
    }

    /**
     * 列表
     */
    public function transferList($params, $page_num, $num)
    {
        $params = is_array($params) ? array_map('trimArray', $params) : '';
        $this->db->select('transfer_record.transfer_id, transfer_record.request_sn, transfer_type, total_amount, total_num, actual_amount, actual_num, success_num, success_amount, transfer_record.status, message, response_content, transfer_record.created_at, updated_at, start_time, status, state');
        $this->db->from($this->_table);

        $this->db->where_in('transfer_type', $params['transfer_type']); // 1,建行全国批量代发代扣交易(6W2100)
        if (!empty($params['transferStatus'])) { //交易状态
            $this->db->where_in("transfer_record.status", $params['transferStatus']);
        }

        if ( ! empty($params['start_date'])) {
            $this->db->where("transfer_record.updated_at >=", $params['start_date']. ' 00:00:01');
        }

        if ( ! empty($params['end_date'])) {
            $this->db->where("transfer_record.updated_at <=", $params['end_date']. ' 23:59:59');
        }

        $this->db->order_by('transfer_id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
}